package com.example.framework.base;

import java.lang.ref.WeakReference;

import com.example.framework.R;
import com.example.framework.interfaces.AlertDialogBtnClickListenerInterface.OnPositiveButtonClickListener;
import com.example.framework.interfaces.AlertDialogBtnClickListenerInterface.OnThreeButtonClickListener;
import com.example.framework.interfaces.AlertDialogBtnClickListenerInterface.OnYesIgnoreButtonClickListener;
import com.example.framework.interfaces.AlertDialogBtnClickListenerInterface.OnYesNoButtonClickListener;
import com.example.framework.interfaces.HandlerInterface;
import com.example.framework.interfaces.AsyncTaskInterface.*;
import com.example.framework.util.UtilOfAsyncTask;
import com.example.framework.util.UtilOfDialog;
import com.example.framework.view.CustomProgressDialog;
import com.google.gson.Gson;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.widget.Toast;

/**
 * 
 * @author renxin.sun
 * 
 */
public abstract class BaseActivity extends Activity
{
	protected Builder builder;
	protected AlertDialog alertDialog;
	protected CustomProgressDialog progressDialog;
	protected MyHandler myHandler;
	protected Gson gson;
	protected long firstTime;
	protected SharedPreferences sharedPreferences;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		gson = new Gson();
		sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		CustomApplication.getInstance().addActivity(this);
	}

	@Override
	public void startActivity(Intent intent)
	{
		super.startActivity(intent);
//		overridePendingTransition(R.anim.fade_enter, R.anim.hold);
//		overridePendingTransition(R.anim.slide_in_enter, R.anim.hold);
		overridePendingTransition(R.anim.magnify_enter, R.anim.fade_exit);
	}

	@Override
	public void startActivityForResult(Intent intent, int requestCode)
	{
		super.startActivityForResult(intent, requestCode);
		overridePendingTransition(R.anim.fade_enter, R.anim.hold);
	}

	@Override
	public void finish()
	{
		super.finish();
//		overridePendingTransition(R.anim.slide_up_in, R.anim.slide_down_out);
		overridePendingTransition(R.anim.hold, R.anim.shrink_exit);
	}

	protected <T> void doAsync(final CallEarliest<T> pCallEarliest, final Callable<T> mCallable, final Callback<T> mCallback)
	{
		UtilOfAsyncTask.doAsync(pCallEarliest, mCallable, mCallback);
	}

	protected <T> void doProgressAsync(final Context pContext, final int styleID, final String pTitleResID, final String pMessageResID, final CallEarliest<T> pCallEarliest, final ProgressCallable<T> pCallable, final Callback<T> pCallback)
	{
		UtilOfAsyncTask.doProgressAsync(pContext, styleID, pTitleResID, pMessageResID, pCallEarliest, pCallable, pCallback);
	}

	protected <T> void doProgressAsync(final Context pContext, final int styleID, final int pTitleResID, final int pMessageResID, final CallEarliest<T> pCallEarliest, final ProgressCallable<T> pCallable, final Callback<T> pCallback)
	{
		UtilOfAsyncTask.doProgressAsync(pContext, styleID, pTitleResID, pMessageResID, pCallEarliest, pCallable, pCallback);
	}

	protected void doPostAsync(final OnPreCall onPreCall, final OnCallBack onCallBack)
	{
		if (null == progressDialog)
		{
			progressDialog = CustomProgressDialog.createDialog(this);
		}
		UtilOfAsyncTask.doPostAsync(this, progressDialog, onPreCall, onCallBack);
	}

	protected void showAlertDialog(CharSequence title, CharSequence message)
	{
		if (null == builder)
		{
			builder = new AlertDialog.Builder(this);
		}
		UtilOfDialog.alertDialogShow(this, builder, title, message).show();
	}

	protected void showAlertDialog(CharSequence title, CharSequence message, CharSequence positiveBtnText, final OnPositiveButtonClickListener listener)
	{
		if (null == builder)
		{
			builder = new AlertDialog.Builder(this);
			builder.setCancelable(false);
		}
		UtilOfDialog.alertDialogShow(this, builder, title, message, positiveBtnText, listener).show();
	}

	protected void showAlertDialog(CharSequence title, CharSequence message, CharSequence positiveBtnText, CharSequence negativeBtnText, final OnYesNoButtonClickListener listener)
	{
		if (null == builder)
		{
			builder = new AlertDialog.Builder(this);
			builder.setCancelable(false);
		}
		UtilOfDialog.alertDialogShow(this, builder, title, message, positiveBtnText, negativeBtnText, listener).show();
	}

	protected void showAlertDialog(CharSequence title, CharSequence message, CharSequence positiveBtnText, CharSequence neutralBtnText, final OnYesIgnoreButtonClickListener listener)
	{
		if (null == builder)
		{
			builder = new AlertDialog.Builder(this);
			builder.setCancelable(false);
		}
		UtilOfDialog.alertDialogShow(this, builder, title, message, positiveBtnText, neutralBtnText, listener).show();
	}

	protected void showAlertDialog(CharSequence title, CharSequence message, CharSequence positiveBtnText, CharSequence negativeBtnText, CharSequence neutralBtnText, final OnThreeButtonClickListener listener)
	{
		if (null == builder)
		{
			builder = new AlertDialog.Builder(this);
			builder.setCancelable(false);
		}
		UtilOfDialog.alertDialogShow(this, builder, title, message, positiveBtnText, negativeBtnText, neutralBtnText, listener).show();
	}

	protected void showAlertDialog(int titleId, int messageId)
	{
		if (null == builder)
		{
			builder = new AlertDialog.Builder(this);
		}
		UtilOfDialog.alertDialogShow(this, builder, titleId, messageId).show();
	}

	protected void showAlertDialog(int titleId, int messageId, int positiveBtnTextId, final OnPositiveButtonClickListener listener)
	{
		if (null == builder)
		{
			builder = new AlertDialog.Builder(this);
			builder.setCancelable(false);
		}
		UtilOfDialog.alertDialogShow(this, builder, titleId, messageId, positiveBtnTextId, listener).show();
	}

	protected void showAlertDialog(int titleId, int messageId, int positiveBtnTextId, int negativeBtnTextId, final OnYesNoButtonClickListener listener)
	{
		if (null == builder)
		{
			builder = new AlertDialog.Builder(this);
			builder.setCancelable(false);
		}
		UtilOfDialog.alertDialogShow(this, builder, titleId, messageId, positiveBtnTextId, negativeBtnTextId, listener).show();
	}

	protected void showAlertDialog(int titleId, int messageId, int positiveBtnTextId, int neutralBtnTextId, final OnYesIgnoreButtonClickListener listener)
	{
		if (null == builder)
		{
			builder = new AlertDialog.Builder(this);
			builder.setCancelable(false);
		}
		UtilOfDialog.alertDialogShow(this, builder, titleId, messageId, positiveBtnTextId, neutralBtnTextId, listener).show();
	}

	protected void showAlertDialog(int titleId, int messageId, int positiveBtnTextId, int negativeBtnTextId, int neutralBtnTextId, final OnThreeButtonClickListener listener)
	{
		if (null == builder)
		{
			builder = new AlertDialog.Builder(this);
			builder.setCancelable(false);
		}
		UtilOfDialog.alertDialogShow(this, builder, titleId, messageId, positiveBtnTextId, negativeBtnTextId, neutralBtnTextId, listener).show();
	}

	protected void dismissAlertDialog()
	{
		UtilOfDialog.dismissAlertDialog(alertDialog);
	}

	protected void showProgressDialog()
	{
		if (null == progressDialog)
		{
			progressDialog = CustomProgressDialog.createDialog(this);
		}
		UtilOfDialog.showProgressDialog(this, progressDialog);
	}

	protected void dismissProgressDialog()
	{
		UtilOfDialog.dismissProgressDialog(progressDialog);
	}

	protected void showToast(CharSequence text)
	{
		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
	}

	protected void showToast(int resId)
	{
		Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
	}

	protected static class MyHandler extends Handler
	{
		private final WeakReference<BaseActivity> mActivity;
		private HandlerInterface handlerInterface;

		public MyHandler(BaseActivity _activity, HandlerInterface _handlerInterface)
		{
			mActivity = new WeakReference<BaseActivity>(_activity);
			handlerInterface = _handlerInterface;
		}

		@Override
		public void handleMessage(Message msg)
		{
			final BaseActivity activity = mActivity.get();
			if (activity != null)
			{
				handlerInterface.handleMessage(msg);
			}
		}
	}

}
